home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / sub_inst.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  5KB  |  166 lines

  1. /************************************************************************
  2. *                                                                       *
  3. * The SB-Prolog System                                                  *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987      *
  5. *                                                                       *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* sub_inst.c */
  25.  
  26. #include <signal.h>
  27. #include "sim.h"
  28. #include "aux.h"
  29.  
  30. /* #define DEBUG */
  31.  
  32. struct sigvec vec;
  33.  
  34. /******************************************************************************/
  35.  
  36. /* set interrupt code in reg 2 and return ep of interrupt handler */
  37.  
  38. LONG_PTR set_intercode(intcode)
  39. WORD intcode;
  40. {
  41.  
  42. #ifdef DEBUG
  43.    printf("set_intercode: intcode = %d  interrupt_psc = %08x\n", 
  44.           intcode, interrupt_psc);
  45. #endif
  46.  
  47.    if (!IS_PRED(interrupt_psc))
  48.       quit("Interrupt handler not defined\n");
  49.  
  50.    reg[2] = MAKEINT(intcode);
  51.    return (LONG_PTR)GET_EP(interrupt_psc);
  52. }
  53.  
  54. /******************************************************************************/
  55.  
  56. intercept_proc()
  57. {
  58.    if (interrupt_code == 0) {
  59.       interrupt_code = 1;
  60.    }
  61.    else exit(2);
  62. }
  63.  
  64. /******************************************************************************/
  65.  
  66. arm_intercept()
  67. {
  68.    /* set up interrupt routine */
  69.    vec.sv_handler = intercept_proc;
  70.    vec.sv_mask = 0;
  71.    vec.sv_onstack = 0;
  72.    sigvec(2, &vec, 0);
  73. }
  74.  
  75. /******************************************************************************/
  76.  
  77. callv_sub()   /* arg from register 1 */
  78. {
  79.    LONG              term;
  80.    register LONG_PTR top;
  81.    WORD              i;
  82.    PSC_REC_PTR       psc_ptr;
  83.    CHAR              s1[256];
  84.  
  85.    term = reg[1];
  86. cvlab: 
  87.    switch (TAG(term)) {
  88.       case FREE: NDEREF(term, cvlab);
  89.       case NUM : printf("illegal call\n");
  90.                  FAIL0;
  91.                  return;
  92.       case LIST: psc_ptr = list_psc;
  93.                  term -= 4;
  94.                  break;
  95.       case CS  : psc_ptr = GET_STR_PSC(term);
  96.                  break;
  97.    }
  98.  
  99.    if (interrupt_code > 0) {
  100.       interrupt_code = 0;
  101.       pcreg = (WORD_PTR)set_intercode(1);
  102.       return;
  103.    }
  104.  
  105.    /* call code */
  106.  
  107.    if (IS_PRED(psc_ptr) || IS_DYNA(psc_ptr))
  108.       pcreg = (WORD_PTR)GET_EP(psc_ptr);
  109.    else {
  110.       pcreg = (WORD_PTR)set_intercode(0);
  111.       return;
  112.    }
  113.    UNTAG(term);
  114.    for (i = 1; i <= GET_ARITY(psc_ptr); ++i)
  115.       reg[i] = FOLLOW((LONG_PTR)term + i);
  116.    if (hitrace == 1) {
  117.       printf("callv: ");
  118.       writepname(stdout, GET_NAME(psc_ptr), GET_LENGTH(psc_ptr));
  119.       printf("        (");
  120.       for (i = 1; i <= GET_ARITY(psc_ptr); i++) {
  121.          printf(" ");
  122.          printterm(reg[i], CAR);
  123.       }
  124.       printf(")\n");
  125.    }
  126.  
  127. }  /* callv_sub */
  128.  
  129. /******************************************************************************/
  130.  
  131. /* builds the current call onto the heap and points reg 1 to it, 
  132.    and puts the interrupt number in reg 2 
  133. */
  134.  
  135. build_call(psc_ptr)
  136. PSC_REC_PTR psc_ptr;
  137. {
  138.    register LONG     callstr, arg;
  139.    register WORD     i;
  140.    register LONG_PTR top;
  141.  
  142. #ifdef DEBUG
  143.    printf("build call : psc = %08x\n", psc_ptr);
  144. #endif
  145.  
  146.    callstr = (LONG)hreg;               /* save addr of new structure rec */
  147.    NEW_HEAP_NODE((LONG)psc_ptr);       /* set str psc ptr */
  148.    for (i = 1; i <= GET_ARITY(psc_ptr); i++) {
  149.       arg = reg[i];
  150.  
  151. #ifdef DEBUG
  152.       printf("build call : arg = %08x  %08x\n", arg , *(LONG_PTR)arg);
  153. #endif
  154.  
  155. bldc: 
  156.       if (ISVAR(arg)) {
  157.          NDEREF(arg, bldc);
  158.          FOLLOW(arg) = (LONG)hreg;
  159.          PUSHTRAIL(arg);
  160.          NEW_HEAP_FREE;
  161.       } else NEW_HEAP_NODE(arg);
  162.    }
  163.    reg[1] = callstr | CS_TAG;    /* ptr to new structure on heap */
  164.  
  165. }  /* build_call */
  166.